#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
list builtin 类型的 gtest 测试

测试 list 类型的各种方法和操作，验证生成的 C++ 代码正确性
"""

import copy
from gtest import test, EXPECT_EQ, EXPECT_TRUE, EXPECT_FALSE
from typing import Any


@test("ListBuiltin", "AppendItem")
def test_append_item():
    """测试向列表添加元素"""
    numbers = [1, 2, 3]
    numbers.append(4)
    expected = [1, 2, 3, 4]
    EXPECT_EQ(numbers, expected)


@test("ListBuiltin", "InsertAtIndex")
def test_insert_at_index():
    """测试在指定位置插入元素 - list[T] 和 list[Any]"""
    # 场景 1: list[int].insert()
    list1: list[int] = [1, 3, 4]
    list1.insert(1, 2)
    expected1 = [1, 2, 3, 4]
    EXPECT_EQ(list1, expected1)

    # 场景 2: list[Any].insert()
    list2: list = [1, 3, 4]
    list2.insert(1, 2)
    EXPECT_EQ(list2, [1, 2, 3, 4])


@test("ListBuiltin", "RemoveItem")
def test_remove_item():
    """测试移除指定元素 - list[T] 和 list[Any]"""
    # 场景 1: list[int].remove()
    list1: list[int] = [1, 2, 3, 2, 4]
    list1.remove(2)
    expected1 = [1, 3, 2, 4]
    EXPECT_EQ(list1, expected1)

    # 场景 2: list[Any].remove()
    list2: list = [1, 2, 3, 2, 4]
    list2.remove(2)
    expected2 = [1, 3, 2, 4]
    EXPECT_EQ(list2, expected2)

    # 测试使用 Any 类型变量
    v: Any = 3
    list1.remove(v)
    expected1_after = [1, 2, 4]
    EXPECT_EQ(list1, expected1_after)


@test("ListBuiltin", "PopLast")
def test_pop_last():
    """测试弹出最后一个元素"""
    list1 = [1, 2, 3, 4]
    result: int = list1.pop()
    expected_result: int = 4
    expected_list = [1, 2, 3]
    EXPECT_EQ(result, expected_result)
    EXPECT_EQ(list1, expected_list)

    list2: list = [1, 2, 3, 4]
    result = list2.pop()
    EXPECT_EQ(result, expected_result)
    EXPECT_EQ(list2, expected_list)


@test("ListBuiltin", "PopAtIndex")
def test_pop_at_index():
    """测试弹出指定位置的元素"""
    list1 = [1, 2, 3, 4]
    result = list1.pop(1)
    expected_result = 2
    expected_list = [1, 3, 4]
    EXPECT_EQ(result, expected_result)
    EXPECT_EQ(list1, expected_list)

    list2: list = [1, 2, 3, 4]
    result = list2.pop(1)
    EXPECT_EQ(result, expected_result)
    EXPECT_EQ(list2, expected_list)


@test("ListBuiltin", "IndexOfItem")
def test_index_of_item():
    """测试查找元素索引 - list[T] 和 list[Any]"""
    # 场景 1: list[int].index()
    list1: list[int] = [10, 20, 30, 20, 40]
    result1: int = list1.index(20)
    expected1: int = 1
    EXPECT_EQ(result1, expected1)

    # 场景 2: list[Any].index()
    list2: list = [10, 20, 30, 20, 40]
    result2: int = list2.index(30)
    expected2: int = 2
    EXPECT_EQ(result2, expected2)


@test("ListBuiltin", "CountOccurrences")
def test_count_occurrences():
    """测试统计元素出现次数 - list[T] 和 list[Any]"""
    # 场景 1: list[int].count()
    list1: list[int] = [1, 2, 2, 3, 2, 4]
    result1: int = list1.count(2)
    expected1: int = 3
    EXPECT_EQ(result1, expected1)

    # 场景 2: list[Any].count()
    list2: list = [1, 2, 2, 3, 2, 4]
    result2: int = list2.count(2)
    expected2: int = 3
    EXPECT_EQ(result2, expected2)

    # 测试列表字面值调用 count 方法
    values = [1, 2, 3]
    c1: int = values.count(1)
    c2: int = [1, 2, 3].count(1)
    EXPECT_EQ(c1, 1)
    EXPECT_EQ(c2, 1)


@test("ListBuiltin", "SortList")
def test_sort_list():
    """测试列表排序 - list[T] 和 list[Any]"""
    # 场景 1: list[int].sort()
    list1: list[int] = [3, 1, 4, 1, 5, 9, 2]
    list1.sort()
    expected1 = [1, 1, 2, 3, 4, 5, 9]
    EXPECT_EQ(list1, expected1)

    # 场景 2: list[Any].sort()
    list2: list = [3, 1, 4, 1, 5, 9, 2]
    list2.sort()
    expected2 = [1, 1, 2, 3, 4, 5, 9]
    EXPECT_EQ(list2, expected2)


@test("ListBuiltin", "ReverseList")
def test_reverse_list():
    """测试列表反转 - list[T] 和 list[Any]"""
    # 场景 1: list[int].reverse()
    list1: list[int] = [1, 2, 3, 4, 5]
    list1.reverse()
    expected1 = [5, 4, 3, 2, 1]
    EXPECT_EQ(list1, expected1)

    # 场景 2: list[Any].reverse()
    list2: list = [1, 2, 3, 4, 5]
    list2.reverse()
    expected2 = [5, 4, 3, 2, 1]
    EXPECT_EQ(list2, expected2)


@test("ListBuiltin", "ClearList")
def test_clear_list():
    """测试清空列表"""
    list1 = [1, 2, 3, 4, 5]
    list1.clear()
    EXPECT_EQ(len(list1), 0)

    list2: list = [1, 2, 3, 4, 5]
    list2.clear()
    EXPECT_EQ(len(list2), 0)


@test("ListBuiltin", "ListLength")
def test_list_length():
    """测试获取列表长度"""
    numbers = [1, 2, 3, 4, 5]
    result: int = len(numbers)
    expected: int = 5
    EXPECT_EQ(result, expected)


@test("ListBuiltin", "ListCopy")
def test_list_copy():
    """测试列表复制"""
    original = [1, 2, 3]
    copied = original.copy()
    EXPECT_EQ(original, copied)

    # 修改原列表不应影响复制的列表
    original.append(4)
    expected_original = [1, 2, 3, 4]
    expected_copied = [1, 2, 3]
    EXPECT_EQ(original, expected_original)
    EXPECT_EQ(copied, expected_copied)


@test("ListBuiltin", "ListEquality")
def test_list_equality():
    """测试列表相等性"""
    list1 = [1, 2, 3]
    list2 = [1, 2, 3]
    list3 = [1, 2, 4]

    EXPECT_TRUE(list1 == list2)
    EXPECT_FALSE(list1 == list3)
    EXPECT_FALSE(list1 != list2)
    EXPECT_TRUE(list1 != list3)


@test("ListBuiltin", "ListBoolean")
def test_list_boolean():
    """测试列表布尔值"""
    empty_list: list[int] = []
    non_empty_list = [1, 2, 3]

    EXPECT_FALSE(bool(empty_list))
    EXPECT_TRUE(bool(non_empty_list))


@test("ListBuiltin", "ListConcatenation")
def test_list_concatenation():
    """测试列表连接"""
    list1: list[int] = [1, 2]
    list2: list[int] = [3, 4]
    list3: list = [5, 6]
    list4: Any = [7, 8]
    result = list1 + list2  # list[int] + list[int]
    result = result + list3  # list[int] + list[Any]
    result = result + list4  # list[int] + Any
    expected = [1, 2, 3, 4, 5, 6, 7, 8]
    EXPECT_EQ(result, expected)

    result1 = list3 + list1  # list[Any] + list[int]
    result1 = result1 + list3  # list[Any] + list[Any]
    result1 = result1 + list4  # list[Any] + Any
    expected = [5, 6, 1, 2, 5, 6, 7, 8]
    EXPECT_EQ(result1, expected)

    result2 = list4 + list1  # Any + list[int]
    result2 = result2 + list3  # Any + list[Any]
    result2 = result2 + list4  # Any + Any
    expected = [7, 8, 1, 2, 5, 6, 7, 8]
    EXPECT_EQ(result2, expected)


@test("ListBuiltin", "EmptyListOperations")
def test_empty_list_operations():
    """测试空列表操作"""
    empty_list: list[int] = []

    # 测试长度
    EXPECT_EQ(len(empty_list), 0)

    # 测试布尔值
    EXPECT_FALSE(bool(empty_list))

    # 测试添加元素
    empty_list.append(1)
    expected: list[int] = [1]
    EXPECT_EQ(empty_list, expected)

    # 测试空列表字面值迭代
    count = 0
    empty_list2: list[int] = []
    for num in empty_list2:
        count = count + 1
    EXPECT_EQ(count, 0)


@test("ListBuiltin", "StringListOperations")
def test_string_list_operations():
    """测试字符串列表操作"""
    words = ["apple", "banana"]
    words.append("orange")
    words.insert(1, "grape")

    expected = ["apple", "grape", "banana", "orange"]
    EXPECT_EQ(words, expected)

    # 测试查找
    index: int = words.index("banana")
    expected_index: int = 2
    EXPECT_EQ(index, expected_index)

    # 直接使用字符串列表字面值
    count = 0
    for word in ["hello", "world", "python"]:
        count = count + 1
    EXPECT_EQ(count, 3)

    # 字符串列表推导式
    words2 = ["apple", "banana", "cherry"]
    upper_words = [w.upper() for w in words2]
    expected_upper = ["APPLE", "BANANA", "CHERRY"]
    EXPECT_EQ(upper_words, expected_upper)


@test("ListBuiltin", "VariantList")
def test_variant_list():
    """测试 variant 列表"""
    numbers = [1, "str", True]
    numbers.append(0.2)
    expected = [1, "str", True, 0.2]
    EXPECT_EQ(numbers, expected)

    # 直接使用混合类型列表字面值
    count = 0
    for item in [1, "str", True, 3.14]:
        count = count + 1
    EXPECT_EQ(count, 4)


@test("ListBuiltin", "EmptyVariantList")
def test_empty_variant_list():
    """测试空 variant 列表"""
    numbers: list = []
    numbers.append(1)
    numbers.append("str")
    numbers.append(True)
    numbers.append(0.2)
    expected = [1, "str", True, 0.2]
    EXPECT_EQ(numbers, expected)


def list_concat(l: list[int], r: list[int]) -> list[int]:
    """测试列表追加"""
    tmp: list[int] = l
    tmp.extend(r)
    return tmp


@test("ListBuiltin", "LiteralList")
def test_literal_list():
    """测试字面值列表类型推导"""
    numbers: list[int] = list_concat([1, 2], [3, 4])
    expected = [1, 2, 3, 4]
    EXPECT_EQ(numbers, expected)


def get_any_list_item(lhs: list, i: int) -> int:
    return lhs[i]


def get_int_list_item(lhs: list[int], i: int) -> int:
    return lhs[i]


@test("ListBuiltin", "IndexAccess")
def test_index_access():
    """测试索引访问"""
    list1 = [10, 20, 30, 40, 50]

    # 正向索引
    EXPECT_EQ(list1[0], 10)
    EXPECT_EQ(list1[2], 30)
    EXPECT_EQ(list1[4], 50)

    # 负向索引
    EXPECT_EQ(list1[-1], 50)
    EXPECT_EQ(list1[-2], 40)
    EXPECT_EQ(list1[-5], 10)

    # 验证在 lambda 中使用索引访问
    get_list1_item = lambda i: list1[i]
    EXPECT_EQ(get_list1_item(2), 30)

    # 验证强类型和弱类型列表使用索引访问
    EXPECT_EQ(get_int_list_item(list1, -2), 40)
    EXPECT_EQ(get_any_list_item(list1, -5), 10)

    list2: list = [10, 20, 30, 40, 50]
    EXPECT_EQ(list2[0], 10)
    EXPECT_EQ(list2[2], 30)
    EXPECT_EQ(list2[4], 50)
    EXPECT_EQ(list2[-1], 50)
    EXPECT_EQ(list2[-2], 40)
    EXPECT_EQ(list2[-5], 10)

    EXPECT_EQ(get_any_list_item(list2, 2), 30)
    EXPECT_EQ(get_any_list_item(list2, -2), 40)


@test("ListBuiltin", "IndexAssignment")
def test_index_assignment():
    """测试索引赋值"""
    list1 = [1, 2, 3, 4, 5]

    # 正向索引赋值
    list1[0] = 10
    list1[2] = 30
    expected1 = [10, 2, 30, 4, 5]
    EXPECT_EQ(list1, expected1)

    # 负向索引赋值
    list1[-1] = 50
    list1[-2] = 40
    expected2 = [10, 2, 30, 40, 50]
    EXPECT_EQ(list1, expected2)

    # list[Any] 弱类型测试
    list2: list = [1, 2, 3, 4, 5]
    list2[0] = 10
    list2[2] = 30
    EXPECT_EQ(list2, expected1)

    list2[-1] = 50
    list2[-2] = 40
    EXPECT_EQ(list2, expected2)


@test("ListBuiltin", "IndexDeletion")
def test_index_deletion():
    """测试索引删除"""
    list1 = [1, 2, 3, 4, 5]

    # 正向索引删除
    del list1[2]
    expected1 = [1, 2, 4, 5]
    EXPECT_EQ(list1, expected1)

    # 负向索引删除
    del list1[-1]
    expected2 = [1, 2, 4]
    EXPECT_EQ(list1, expected2)

    # list[Any] 弱类型测试
    list2: list = [1, 2, 3, 4, 5]
    del list2[2]
    EXPECT_EQ(list2, expected1)

    del list2[-1]
    EXPECT_EQ(list2, expected2)


@test("ListBuiltin", "MembershipTest")
def test_membership_test():
    """测试成员检测（in/not in）- list[T] 和 list[Any]"""
    # 场景 1: list[int]
    list1: list[int] = [1, 2, 3, 4, 5]
    result1: bool = 3 in list1
    result2: bool = 10 in list1
    EXPECT_TRUE(result1)
    EXPECT_FALSE(result2)

    # 场景 2: list[Any]
    list2: list = [1, 2, 3, 4, 5]
    result3: bool = 3 in list2
    result4: bool = 10 in list2
    EXPECT_TRUE(result3)
    EXPECT_FALSE(result4)

    # 直接使用列表字面值测试成员检测
    result5: bool = 3 in [1, 2, 3, 4, 5]
    result6: bool = 10 in [1, 2, 3, 4, 5]
    EXPECT_TRUE(result5)
    EXPECT_FALSE(result6)


@test("ListBuiltin", "ListComparison")
def test_list_comparison():
    """测试列表比较操作"""
    list1 = [1, 2, 3]
    list2 = [1, 2, 4]
    list3 = [1, 2, 3, 4]
    list4 = [1, 2, 3]

    # 小于
    EXPECT_TRUE(list1 < list2)
    EXPECT_TRUE(list1 < list3)
    EXPECT_FALSE(list2 < list1)

    # 小于等于
    EXPECT_TRUE(list1 <= list2)
    EXPECT_TRUE(list1 <= list4)
    EXPECT_FALSE(list2 <= list1)

    # 大于
    EXPECT_TRUE(list2 > list1)
    EXPECT_TRUE(list3 > list1)
    EXPECT_FALSE(list1 > list2)

    # 大于等于
    EXPECT_TRUE(list2 >= list1)
    EXPECT_TRUE(list1 >= list4)
    EXPECT_FALSE(list1 >= list2)


@test("ListBuiltin", "ListMultiplication")
def test_list_multiplication():
    """测试列表乘法运算符 (*) - list[T] 和 Any 类型"""
    # 场景 1: list[int] * int
    list1: list[int] = [1, 2]
    result1 = list1 * 3
    expected1 = [1, 2, 1, 2, 1, 2]
    EXPECT_EQ(result1, expected1)

    # # 场景 2: Any * int (运算符重载)
    # list2: Any = [1, 2]
    # result2 = list2 * 3
    # expected2 = [1, 2, 1, 2, 1, 2]
    # EXPECT_EQ(result2, expected2)

    # 测试乘以 0
    result_zero: list[int] = list1 * 0
    expected_zero: list[int] = []
    EXPECT_EQ(result_zero, expected_zero)


@test("ListBuiltin", "ListInplaceMultiplication")
def test_list_inplace_multiplication():
    """测试列表原地乘法运算符 (*=) - list[T] 和 Any 类型"""
    # 场景 1: list[int] *= int
    list1: list[int] = [1, 2]
    list1 *= 3
    expected1 = [1, 2, 1, 2, 1, 2]
    EXPECT_EQ(list1, expected1)

    # # 场景 2: Any *= int (运算符重载)
    # list2: Any = [1, 2]
    # list2 *= 3
    # expected2 = [1, 2, 1, 2, 1, 2]
    # EXPECT_EQ(list2, expected2)

    # 测试乘以 0
    list_zero: list[int] = [1, 2, 3]
    list_zero *= 0
    expected_zero: list[int] = []
    EXPECT_EQ(list_zero, expected_zero)


@test("ListBuiltin", "ListInplaceAddition")
def test_list_inplace_addition():
    """测试列表原地加法 (+=) 各种类型组合"""
    # 场景 1: list[int] += list[int]
    list1: list[int] = [1, 2]
    list1 += [3, 4]
    expected1 = [1, 2, 3, 4]
    EXPECT_EQ(list1, expected1)

    # 场景 2: list[int] += list[Any]
    list2: list[int] = [1, 2]
    list_any: list = [3, 4]
    list2 += list_any
    expected2 = [1, 2, 3, 4]
    EXPECT_EQ(list2, expected2)

    # 场景 3: list[int] += Any
    list3: list[int] = [1, 2]
    any_list: Any = [3, 4]
    list3 += any_list
    expected3 = [1, 2, 3, 4]
    EXPECT_EQ(list3, expected3)

    # 场景 4: list[Any] += list[int]
    list4: list = [1, 2]
    int_list: list[int] = [3, 4]
    list4 += int_list
    expected4 = [1, 2, 3, 4]
    EXPECT_EQ(list4, expected4)

    # 场景 5: list[Any] += list[Any]
    list5: list = [1, 2]
    list5_other: list = [3, 4]
    list5 += list5_other
    expected5 = [1, 2, 3, 4]
    EXPECT_EQ(list5, expected5)

    # 场景 6: list[Any] += Any
    list6: list = [1, 2]
    any_list2: Any = [3, 4]
    list6 += any_list2
    expected6 = [1, 2, 3, 4]
    EXPECT_EQ(list6, expected6)

    # 场景 7: Any += list[int]
    list7: Any = [1, 2]
    list7 += [3, 4]
    expected7 = [1, 2, 3, 4]
    EXPECT_EQ(list7, expected7)

    # 场景 8: Any += list[Any]
    list8: Any = [1, 2]
    list8_other: list = [3, 4]
    list8 += list8_other
    expected8 = [1, 2, 3, 4]
    EXPECT_EQ(list8, expected8)

    # 场景 9: Any += Any
    list9: Any = [1, 2]
    any_list3: Any = [3, 4]
    list9 += any_list3
    expected9 = [1, 2, 3, 4]
    EXPECT_EQ(list9, expected9)

    # 综合场景：多次 += 操作
    result: list[int] = [1, 2]
    result += [3, 4]  # list[int] += list[int]
    result += list_any  # list[int] += list[Any]
    result += any_list  # list[int] += Any
    expected_final = [1, 2, 3, 4, 3, 4, 3, 4]
    EXPECT_EQ(result, expected_final)


@test("ListBuiltin", "ListExtend")
def test_list_extend():
    """测试列表 extend 方法各种类型组合"""
    # 场景 1: list[int].extend(list[int])
    list1: list[int] = [1, 2]
    list1.extend([3, 4])
    expected1 = [1, 2, 3, 4]
    EXPECT_EQ(list1, expected1)

    # 场景 2: list[int].extend(list[Any])
    list2: list[int] = [1, 2]
    list_any: list = [3, 4]
    list2.extend(list_any)
    expected2 = [1, 2, 3, 4]
    EXPECT_EQ(list2, expected2)

    # 场景 3: list[int].extend(Any)
    list3: list[int] = [1, 2]
    any_list: Any = [3, 4]
    list3.extend(any_list)
    expected3 = [1, 2, 3, 4]
    EXPECT_EQ(list3, expected3)

    # 场景 4: list[Any].extend(list[int])
    list4: list = [1, 2]
    int_list: list[int] = [3, 4]
    list4.extend(int_list)
    expected4 = [1, 2, 3, 4]
    EXPECT_EQ(list4, expected4)

    # 场景 5: list[Any].extend(list[Any])
    list5: list = [1, 2]
    list5_other: list = [3, 4]
    list5.extend(list5_other)
    expected5 = [1, 2, 3, 4]
    EXPECT_EQ(list5, expected5)

    # 场景 6: list[Any].extend(Any)
    list6: list = [1, 2]
    any_list2: Any = [3, 4]
    list6.extend(any_list2)
    expected6 = [1, 2, 3, 4]
    EXPECT_EQ(list6, expected6)

    # 综合场景：多次 extend 操作
    result: list[int] = [1, 2]
    result.extend([3, 4])  # list[int].extend(list[int])
    result.extend(list_any)  # list[int].extend(list[Any])
    result.extend(any_list)  # list[int].extend(Any)
    expected_final = [1, 2, 3, 4, 3, 4, 3, 4]
    EXPECT_EQ(result, expected_final)


@test("ListBuiltin", "ListRepr")
def test_list_repr():
    """测试列表字符串表示"""
    numbers = [1, 2, 3, "a", "b"]
    result: str = repr(numbers)
    expected: str = '[1,2,3,"a","b"]'
    EXPECT_EQ(result, expected)

    # 测试弱类型 list
    numbers_1: list = [1, 2, 3, "a", "b"]
    result_1: str = repr(numbers_1)
    EXPECT_EQ(result_1, expected)


@test("ListBuiltin", "ListStr")
def test_list_str():
    """测试列表字符串转换"""
    numbers = [10, 20, 30]
    result: str = str(numbers)
    expected: str = "[10,20,30]"
    EXPECT_EQ(result, expected)

    # 测试弱类型 list
    numbers_1: list = [10, 20, 30]
    result_1: str = str(numbers_1)
    EXPECT_EQ(result, expected)


@test("ListBuiltin", "EmptyListRepr")
def test_empty_list_repr():
    """测试空列表字符串表示"""
    empty: list[int] = []
    result: str = repr(empty)
    expected: str = "[]"
    EXPECT_EQ(result, expected)


@test("ListBuiltin", "StringListMembership")
def test_string_list_membership():
    """测试字符串列表成员检测"""
    words = ["apple", "banana", "orange"]

    # 使用 in 运算符
    result1: bool = "apple" in words
    result2: bool = "banana" in words
    result3: bool = "grape" in words

    EXPECT_TRUE(result1)
    EXPECT_TRUE(result2)
    EXPECT_FALSE(result3)


@test("ListBuiltin", "StringListIndexAccess")
def test_string_list_index_access():
    """测试字符串列表索引访问"""
    words = ["hello", "world", "python"]

    EXPECT_EQ(words[0], "hello")
    EXPECT_EQ(words[-1], "python")

    # 字符串字面量已经是 str 类型
    new_word: str = "beautiful"
    words[1] = new_word
    expected = ["hello", "beautiful", "python"]
    EXPECT_EQ(words, expected)


@test("ListBuiltin", "MixedOperations")
def test_mixed_operations():
    """测试混合操作"""
    numbers = [1, 2, 3]

    # 连接后索引访问
    result: list[int] = numbers + [4, 5]
    EXPECT_EQ(result[3], 4)
    EXPECT_EQ(result[-1], 5)

    # 重复后成员检测
    repeated: list[int] = numbers.__mul__(2)
    contains_result: bool = 1 in repeated
    EXPECT_TRUE(contains_result)
    count_result: int = repeated.count(1)
    EXPECT_EQ(count_result, 2)


@test("ListBuiltin", "SortWithReverse")
def test_sort_with_reverse():
    """测试带reverse参数的sort方法"""
    list1 = [3, 1, 4, 1, 5, 9, 2, 6]

    # 升序排序
    list1.sort()
    expected_asc = [1, 1, 2, 3, 4, 5, 6, 9]
    EXPECT_EQ(list1, expected_asc)

    # 降序排序
    list1.sort(reverse=True)
    expected_desc = [9, 6, 5, 4, 3, 2, 1, 1]
    EXPECT_EQ(list1, expected_desc)

    # 使用reverse=False明确指定升序
    list1.sort(reverse=False)
    EXPECT_EQ(list1, expected_asc)

    list2: list = [3, 1, 4, 1, 5, 9, 2, 6]  # list[Any]
    list2.sort()
    EXPECT_EQ(list2, expected_asc)
    list2.sort(reverse=True)
    EXPECT_EQ(list2, expected_desc)
    list2.sort(reverse=False)
    EXPECT_EQ(list2, expected_asc)


@test("ListBuiltin", "IndexWithRange")
def test_index_with_range():
    """测试带start和end参数的index方法"""
    list1 = [1, 2, 3, 2, 4, 2, 5]
    idx1 = list1.index(2)
    EXPECT_EQ(idx1, 1)  # 找到第一个2
    idx2 = list1.index(2, 2, 7)
    EXPECT_EQ(idx2, 3)  # 找到索引3处的2
    idx3 = list1.index(2, 4, 7)
    EXPECT_EQ(idx3, 5)  # 找到索引5处的2
    idx4 = list1.index(2, -3, -1)
    EXPECT_EQ(idx4, 5)  # 在范围[4:6]中找到索引5处的2

    list2: list = [1, 2, 3, 2, 4, 2, 5]
    idx1 = list2.index(2)
    EXPECT_EQ(idx1, 1)  # 找到第一个2
    idx2 = list2.index(2, 2, 7)
    EXPECT_EQ(idx2, 3)  # 找到索引3处的2
    idx3 = list2.index(2, 4, 7)
    EXPECT_EQ(idx3, 5)  # 找到索引5处的2
    idx4 = list2.index(2, -3, -1)
    EXPECT_EQ(idx4, 5)  # 在范围[4:6]中找到索引5处的2


@test("ListBuiltin", "SortStrings")
def test_sort_strings():
    """测试字符串列表排序"""
    words = ["banana", "apple", "cherry", "date"]

    # 升序排序
    words.sort()
    expected_asc = ["apple", "banana", "cherry", "date"]
    EXPECT_EQ(words, expected_asc)

    # 降序排序
    words.sort(reverse=True)
    expected_desc = ["date", "cherry", "banana", "apple"]
    EXPECT_EQ(words, expected_desc)


@test("ListBuiltin", "KeywordArgumentsWithSort")
def test_keyword_arguments_with_sort():
    """测试关键字参数支持"""
    # 测试 sort 方法的关键字参数
    numbers1 = [3, 1, 4, 1, 5, 9, 2, 6]
    numbers1.sort(reverse=True)
    expected_desc = [9, 6, 5, 4, 3, 2, 1, 1]
    EXPECT_EQ(numbers1, expected_desc)

    # 测试 sort 方法的关键字参数为 False
    numbers2 = [3, 1, 4, 1, 5, 9, 2, 6]
    numbers2.sort(reverse=False)
    expected_asc = [1, 1, 2, 3, 4, 5, 6, 9]
    EXPECT_EQ(numbers2, expected_asc)

    # 测试多次调用
    numbers3 = [5, 2, 8, 1]
    numbers3.sort(reverse=True)
    EXPECT_EQ(numbers3[0], 8)
    numbers3.sort(reverse=False)
    EXPECT_EQ(numbers3[0], 1)


@test("ListBuiltin", "SliceBasic")
def test_slice_basic():
    """测试基本切片功能"""
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    # 基本切片
    slice1: list[int] = numbers[2:5]
    expected1 = [2, 3, 4]
    EXPECT_EQ(slice1, expected1)

    # 从开始切片
    slice2: list[int] = numbers[:3]
    expected2 = [0, 1, 2]
    EXPECT_EQ(slice2, expected2)

    # 到结尾切片
    slice3: list[int] = numbers[7:]
    expected3 = [7, 8, 9]
    EXPECT_EQ(slice3, expected3)

    # 完整切片（复制）
    slice4: list[int] = numbers[:]
    EXPECT_EQ(slice4, numbers)


@test("ListBuiltin", "SliceWithStep")
def test_slice_with_step():
    """测试带步长的切片"""
    list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    # 步长为2
    slice1: list[int] = list1[::2]
    expected1 = [0, 2, 4, 6, 8]
    EXPECT_EQ(slice1, expected1)

    # 指定范围和步长
    slice2: list[int] = list1[1:8:2]
    expected2 = [1, 3, 5, 7]
    EXPECT_EQ(slice2, expected2)

    # 负步长（反向）
    slice3: list[int] = list1[::-1]
    expected3 = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    EXPECT_EQ(slice3, expected3)

    # 反向切片
    slice4: list[int] = list1[8:2:-2]
    expected4 = [8, 6, 4]
    EXPECT_EQ(slice4, expected4)

    list2: list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    slice2_1 = list2[::2]
    EXPECT_EQ(slice2_1, expected1)
    slice2_2 = list2[1:8:2]
    EXPECT_EQ(slice2_2, expected2)
    slice2_3 = list2[::-1]
    EXPECT_EQ(slice2_3, expected3)

    # 构造一次list[Any] -> list[int] 的类型转换
    slice2_4: list[int] = list2[8:2:-2]
    EXPECT_EQ(slice2_4, expected4)


@test("ListBuiltin", "SliceNegativeIndices")
def test_slice_negative_indices():
    """测试负索引切片"""
    numbers = [0, 1, 2, 3, 4, 5]

    # 负索引切片
    slice1: list[int] = numbers[-3:-1]
    expected1 = [3, 4]
    EXPECT_EQ(slice1, expected1)

    # 混合正负索引
    slice2: list[int] = numbers[1:-1]
    expected2 = [1, 2, 3, 4]
    EXPECT_EQ(slice2, expected2)

    # 负索引到结尾
    slice3: list[int] = numbers[-2:]
    expected3 = [4, 5]
    EXPECT_EQ(slice3, expected3)


@test("ListBuiltin", "SliceAssignment")
def test_slice_assignment():
    """测试切片赋值"""
    # 基本切片赋值
    numbers = [0, 1, 2, 3, 4, 5]
    numbers[1:4] = [10, 20, 30]
    expected1 = [0, 10, 20, 30, 4, 5]
    EXPECT_EQ(numbers, expected1)

    # 切片赋值改变长度
    numbers2 = [0, 1, 2, 3, 4]
    numbers2[1:3] = [10]
    expected2 = [0, 10, 3, 4]
    EXPECT_EQ(numbers2, expected2)

    # 插入元素
    numbers3 = [0, 1, 2]
    numbers3[1:1] = [10, 20]
    expected3 = [0, 10, 20, 1, 2]
    EXPECT_EQ(numbers3, expected3)


@test("ListBuiltin", "AnyListSliceAssignment")
def test_any_slice_assignment():
    """测试切片赋值"""
    # 基本切片赋值
    numbers: list = [0, 1, 2, 3, 4, 5]
    numbers[1:4] = [10, 20, 30]
    expected1 = [0, 10, 20, 30, 4, 5]
    EXPECT_EQ(numbers, expected1)

    # 切片赋值改变长度
    numbers2: list = [0, 1, 2, 3, 4]
    numbers2[1:3] = [10]
    expected2 = [0, 10, 3, 4]
    EXPECT_EQ(numbers2, expected2)

    # 插入元素
    numbers3: list = [0, 1, 2]
    numbers3[1:1] = [10, 20]
    expected3 = [0, 10, 20, 1, 2]
    EXPECT_EQ(numbers3, expected3)


@test("ListBuiltin", "SliceDelete")
def test_slice_delete():
    """测试切片删除"""
    # 基本切片删除
    numbers = [0, 1, 2, 3, 4, 5]
    del numbers[1:4]
    expected1 = [0, 4, 5]
    EXPECT_EQ(numbers, expected1)

    # 删除到结尾
    numbers2 = [0, 1, 2, 3, 4]
    del numbers2[2:]
    expected2 = [0, 1]
    EXPECT_EQ(numbers2, expected2)

    # 删除从开始
    numbers3 = [0, 1, 2, 3]
    del numbers3[:2]
    expected3 = [2, 3]
    EXPECT_EQ(numbers3, expected3)


@test("ListBuiltin", "AnyListSliceDelete")
def test_any_slice_delete():
    """测试切片删除"""
    # 基本切片删除
    numbers: list = [0, 1, 2, 3, 4, 5]
    del numbers[1:4]
    expected1 = [0, 4, 5]
    EXPECT_EQ(numbers, expected1)

    # 删除到结尾
    numbers2: list = [0, 1, 2, 3, 4]
    del numbers2[2:]
    expected2 = [0, 1]
    EXPECT_EQ(numbers2, expected2)

    # 删除从开始
    numbers3: list = [0, 1, 2, 3]
    del numbers3[:2]
    expected3 = [2, 3]
    EXPECT_EQ(numbers3, expected3)


@test("ListBuiltin", "ForInLoop")
def test_for_in_loop():
    """测试 for-in 循环迭代"""
    # 基本迭代求和
    numbers = [1, 2, 3, 4, 5]
    sum_val = 0
    for num in numbers:
        sum_val = sum_val + num
    EXPECT_EQ(sum_val, 15)

    # 直接使用列表字面值迭代
    sum_literal = 0
    for num in [1, 2, 3, 4, 5]:
        sum_literal = sum_literal + num
    EXPECT_EQ(sum_literal, 15)

    # 迭代计数
    count = 0
    words = ["hello", "world", "python"]
    for word in words:
        count = count + 1
    EXPECT_EQ(count, 3)

    # 迭代修改外部列表（注意：不能修改迭代的列表本身）
    result: list[int] = []
    for num in numbers:
        result.append(num * 2)
    expected_result = [2, 4, 6, 8, 10]
    EXPECT_EQ(result, expected_result)


@test("ListBuiltin", "ForInNested")
def test_for_in_nested():
    """测试嵌套 for-in 循环"""
    matrix = [[1, 2], [3, 4], [5, 6]]

    sum_val = 0
    for row in matrix:
        for num in row:
            sum_val = sum_val + num

    EXPECT_EQ(sum_val, 21)

    # 直接使用嵌套列表字面值
    sum_literal = 0
    for row in [[1, 2], [3, 4], [5, 6]]:
        for num in row:
            sum_literal = sum_literal + num
    EXPECT_EQ(sum_literal, 21)


@test("ListBuiltin", "ForInWithBreakContinue")
def test_for_in_with_control():
    """测试 for-in 循环中的控制流"""
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # 测试 break
    sum_val = 0
    for num in numbers:
        if num > 5:
            break
        sum_val = sum_val + num
    EXPECT_EQ(sum_val, 15)  # 1+2+3+4+5

    # 直接使用列表字面值测试 break
    sum_literal = 0
    for num in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
        if num > 5:
            break
        sum_literal = sum_literal + num
    EXPECT_EQ(sum_literal, 15)

    # 测试 continue
    even_sum = 0
    for num in numbers:
        if num % 2 == 1:  # 跳过奇数
            continue
        even_sum = even_sum + num
    EXPECT_EQ(even_sum, 30)  # 2+4+6+8+10


@test("ListBuiltin", "ListComprehensionBasic")
def test_list_comprehension_basic():
    """测试基本列表推导式"""
    numbers = [1, 2, 3, 4, 5]

    # 基本列表推导式（使用弱类型）
    doubled = [x * 2 for x in numbers]
    expected_doubled = [2, 4, 6, 8, 10]
    EXPECT_EQ(doubled, expected_doubled)

    # 带运算的列表推导式
    squares = [x * x for x in numbers]
    expected_squares = [1, 4, 9, 16, 25]
    EXPECT_EQ(squares, expected_squares)


@test("ListBuiltin", "ListComprehensionWithCondition")
def test_list_comprehension_with_condition():
    """测试带条件的列表推导式"""
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    # 筛选偶数
    evens = [x for x in numbers if x % 2 == 0]
    expected_evens = [2, 4, 6, 8, 10]
    EXPECT_EQ(evens, expected_evens)

    # 筛选并变换
    even_squares = [x * x for x in numbers if x % 2 == 0]
    expected_even_squares = [4, 16, 36, 64, 100]
    EXPECT_EQ(even_squares, expected_even_squares)


@test("ListBuiltin", "ListComprehensionNested")
def test_list_comprehension_nested():
    """测试嵌套列表推导式"""
    matrix = [[1, 2], [3, 4], [5, 6]]

    # 展平矩阵（使用弱类型以避免复杂的类型推导）
    flat = [num for row in matrix for num in row]
    expected_flat = [1, 2, 3, 4, 5, 6]
    # 注意：嵌套推导式返回 mc::variants，需要逐元素比较
    EXPECT_EQ(len(flat), 6)
    for i in range(6):
        EXPECT_EQ(flat[i], expected_flat[i])

    # 直接使用列表字面值的嵌套推导式
    flat_literal = [num for row in [[1, 2], [3, 4], [5, 6]] for num in row]
    EXPECT_EQ(len(flat_literal), 6)
    for i in range(6):
        EXPECT_EQ(flat_literal[i], expected_flat[i])


@test("ListBuiltin", "ListConstructor")
def test_list_constructor():
    """测试 list() 构造函数"""
    # 从另一个列表复制
    original = [1, 2, 3, 4, 5]
    copied: list[int] = list(original)
    EXPECT_EQ(copied, original)

    # 修改复制后的列表不影响原列表
    copied.append(6)
    EXPECT_EQ(len(copied), 6)
    EXPECT_EQ(len(original), 5)


@test("ListBuiltin", "ReversedIteration")
def test_reversed_iteration():
    """测试 reversed() 反向迭代"""
    numbers = [1, 2, 3, 4, 5]

    # 使用 reversed() 反向遍历（左值引用版本）
    result: list[int] = []
    for num in reversed(numbers):
        result.append(num)

    expected = [5, 4, 3, 2, 1]
    EXPECT_EQ(result, expected)

    # 测试字符串列表的反向迭代
    words = ["apple", "banana", "cherry"]
    reversed_words: list[str] = []
    for word in reversed(words):
        reversed_words.append(word)
    expected_words = ["cherry", "banana", "apple"]
    EXPECT_EQ(reversed_words, expected_words)

    # 测试列表字面量的反向迭代（右值拷贝版本）
    literal_result: list[int] = []
    for num in reversed([10, 20, 30]):
        literal_result.append(num)
    expected_literal = [30, 20, 10]
    EXPECT_EQ(literal_result, expected_literal)


@test("ListBuiltin", "ShallowCopy")
def test_shallow_copy():
    """测试列表浅拷贝（使用 copy.copy）"""
    # 测试简单列表的浅拷贝
    simple_list = [1, 2, 3, 4, 5]
    shallow_copy_simple: list[int] = copy.copy(simple_list)
    EXPECT_EQ(shallow_copy_simple, simple_list)

    # 修改拷贝不影响原列表
    shallow_copy_simple.append(6)
    EXPECT_EQ(len(simple_list), 5)
    EXPECT_EQ(len(shallow_copy_simple), 6)

    # 测试包含字典的列表（dict 具有引用语义）
    dict_list = [{"a": 1, "b": 2}, {"c": 3, "d": 4}]
    shallow_copy_dict = copy.copy(dict_list)
    EXPECT_EQ(len(shallow_copy_dict), 2)

    # 浅拷贝：修改内层字典会影响原列表（因为字典是引用）
    # 直接通过索引访问并修改
    shallow_copy_dict[0]["a"] = 100

    # 原列表的内层字典也被修改了（浅拷贝共享引用）
    EXPECT_EQ(dict_list[0]["a"], 100)

    # 但添加新的外层元素不影响原列表
    shallow_copy_dict.append({"e": 5})
    EXPECT_EQ(len(dict_list), 2)
    EXPECT_EQ(len(shallow_copy_dict), 3)


@test("ListBuiltin", "DeepCopy")
def test_deep_copy():
    """测试列表深拷贝（使用 copy.deepcopy）"""
    # 测试简单列表的深拷贝
    simple_list = [1, 2, 3, 4, 5]
    deep_copy_simple: list[int] = copy.deepcopy(simple_list)
    EXPECT_EQ(deep_copy_simple, simple_list)

    # 修改拷贝不影响原列表
    deep_copy_simple.append(6)
    EXPECT_EQ(len(simple_list), 5)
    EXPECT_EQ(len(deep_copy_simple), 6)

    # 测试包含字典的列表（dict 具有引用语义）
    dict_list = [{"a": 1, "b": 2}, {"c": 3, "d": 4}]
    deep_copy_dict = copy.deepcopy(dict_list)
    EXPECT_EQ(len(deep_copy_dict), 2)

    # 深拷贝：修改内层字典不会影响原列表（完全独立的副本）
    deep_copy_dict[0]["a"] = 100

    # 原列表的内层字典未被修改（深拷贝是独立的）
    EXPECT_EQ(dict_list[0]["a"], 1)

    # 拷贝的内层字典已修改
    EXPECT_EQ(deep_copy_dict[0]["a"], 100)

    # 添加新的外层元素也不影响原列表
    deep_copy_dict.append({"e": 5})
    EXPECT_EQ(len(dict_list), 2)
    EXPECT_EQ(len(deep_copy_dict), 3)


@test("ListBuiltin", "ShallowCopyNestedList")
def test_shallow_copy_nested_list():
    """测试嵌套列表的浅拷贝（list 具有引用语义）"""
    # 创建嵌套列表（list[list[int]]）
    nested_list = [[1, 2, 3], [4, 5, 6]]
    shallow_copy_nested = copy.copy(nested_list)

    # 验证初始状态相等
    EXPECT_EQ(len(shallow_copy_nested), 2)
    expected_first: list[int] = [1, 2, 3]
    expected_second: list[int] = [4, 5, 6]
    EXPECT_EQ(shallow_copy_nested[0], expected_first)
    EXPECT_EQ(shallow_copy_nested[1], expected_second)

    # 浅拷贝：修改内层列表会影响原列表（因为内层列表是引用）
    shallow_copy_nested[0].append(999)

    # 原列表的内层列表也被修改了（浅拷贝共享内层引用）
    EXPECT_EQ(len(nested_list[0]), 4)
    EXPECT_EQ(nested_list[0][3], 999)
    EXPECT_EQ(len(shallow_copy_nested[0]), 4)
    EXPECT_EQ(shallow_copy_nested[0][3], 999)

    # 但替换外层元素不影响原列表
    new_list: list[int] = [7, 8, 9]
    shallow_copy_nested[1] = new_list
    EXPECT_EQ(nested_list[1], expected_second)  # 原列表未变
    EXPECT_EQ(shallow_copy_nested[1], new_list)  # 拷贝已变

    # 添加新的外层元素不影响原列表
    appended_list: list[int] = [10, 11, 12]
    shallow_copy_nested.append(appended_list)
    EXPECT_EQ(len(nested_list), 2)
    EXPECT_EQ(len(shallow_copy_nested), 3)


@test("ListBuiltin", "DeepCopyNestedList")
def test_deep_copy_nested_list():
    """测试嵌套列表的深拷贝（list 具有引用语义）"""
    # 创建嵌套列表（list[list[int]]）
    nested_list = [[1, 2, 3], [4, 5, 6]]
    deep_copy_nested = copy.deepcopy(nested_list)

    # 验证初始状态相等
    expected_first: list[int] = [1, 2, 3]
    expected_second: list[int] = [4, 5, 6]
    EXPECT_EQ(len(deep_copy_nested), 2)
    EXPECT_EQ(deep_copy_nested[0], expected_first)
    EXPECT_EQ(deep_copy_nested[1], expected_second)

    # 深拷贝：修改内层列表不会影响原列表（完全独立的副本）
    deep_copy_nested[0].append(999)

    # 原列表的内层列表未被修改（深拷贝是独立的）
    EXPECT_EQ(len(nested_list[0]), 3)
    EXPECT_EQ(nested_list[0], expected_first)

    # 拷贝的内层列表已修改
    EXPECT_EQ(len(deep_copy_nested[0]), 4)
    EXPECT_EQ(deep_copy_nested[0][3], 999)

    # 替换外层元素也不影响原列表
    new_list: list[int] = [7, 8, 9]
    deep_copy_nested[1] = new_list
    EXPECT_EQ(nested_list[1], expected_second)  # 原列表未变
    EXPECT_EQ(deep_copy_nested[1], new_list)  # 拷贝已变

    # 添加新的外层元素也不影响原列表
    appended_list: list[int] = [10, 11, 12]
    deep_copy_nested.append(appended_list)
    EXPECT_EQ(len(nested_list), 2)
    EXPECT_EQ(len(deep_copy_nested), 3)


@test("ListBuiltin", "ShallowCopySetList")
def test_shallow_copy_set_list():
    """测试包含 set 的列表浅拷贝（set 具有引用语义）"""
    # 创建包含 set 的列表（list[set[int]]）
    set_list: list[set[int]] = [{1, 2, 3}, {4, 5, 6}]
    shallow_copy_set = copy.copy(set_list)

    # 验证初始状态相等
    EXPECT_EQ(len(shallow_copy_set), 2)
    EXPECT_TRUE(1 in shallow_copy_set[0])
    EXPECT_TRUE(4 in shallow_copy_set[1])

    # 浅拷贝：修改内层 set 会影响原列表（因为 set 是引用）
    shallow_copy_set[0].add(999)

    # 原列表的内层 set 也被修改了（浅拷贝共享内层引用）
    EXPECT_TRUE(999 in set_list[0])
    EXPECT_TRUE(999 in shallow_copy_set[0])

    # 但替换外层元素不影响原列表
    shallow_copy_set[1] = {7, 8, 9}
    EXPECT_TRUE(4 in set_list[1])  # 原列表未变
    EXPECT_TRUE(7 in shallow_copy_set[1])  # 拷贝已变
    EXPECT_FALSE(4 in shallow_copy_set[1])

    # 添加新的外层元素不影响原列表
    shallow_copy_set.append({10, 11, 12})
    EXPECT_EQ(len(set_list), 2)
    EXPECT_EQ(len(shallow_copy_set), 3)


@test("ListBuiltin", "DeepCopySetList")
def test_deep_copy_set_list():
    """测试包含 set 的列表深拷贝（set 具有引用语义）"""
    # 创建包含 set 的列表（list[set[int]]）
    set_list: list[set[int]] = [{1, 2, 3}, {4, 5, 6}]
    deep_copy_set = copy.deepcopy(set_list)

    # 验证初始状态相等
    EXPECT_EQ(len(deep_copy_set), 2)
    EXPECT_TRUE(1 in deep_copy_set[0])
    EXPECT_TRUE(4 in deep_copy_set[1])

    # 深拷贝：修改内层 set 不会影响原列表（完全独立的副本）
    deep_copy_set[0].add(999)

    # 原列表的内层 set 未被修改（深拷贝是独立的）
    EXPECT_FALSE(999 in set_list[0])
    EXPECT_TRUE(1 in set_list[0])

    # 拷贝的内层 set 已修改
    EXPECT_TRUE(999 in deep_copy_set[0])

    # 替换外层元素也不影响原列表
    deep_copy_set[1] = {7, 8, 9}
    EXPECT_TRUE(4 in set_list[1])  # 原列表未变
    EXPECT_TRUE(7 in deep_copy_set[1])  # 拷贝已变

    # 添加新的外层元素也不影响原列表
    deep_copy_set.append({10, 11, 12})
    EXPECT_EQ(len(set_list), 2)
    EXPECT_EQ(len(deep_copy_set), 3)


@test("ListBuiltin", "CopyEmptyList")
def test_copy_empty_list():
    """测试空列表的拷贝"""
    # 测试空列表的浅拷贝
    empty_list: list[int] = []
    shallow_copy_empty = copy.copy(empty_list)
    EXPECT_EQ(len(shallow_copy_empty), 0)

    # 修改拷贝不影响原列表
    shallow_copy_empty.append(1)
    EXPECT_EQ(len(empty_list), 0)
    EXPECT_EQ(len(shallow_copy_empty), 1)

    # 测试空列表的深拷贝
    empty_list2: list[int] = []
    deep_copy_empty = copy.deepcopy(empty_list2)
    EXPECT_EQ(len(deep_copy_empty), 0)

    # 修改拷贝不影响原列表
    deep_copy_empty.append(2)
    EXPECT_EQ(len(empty_list2), 0)
    EXPECT_EQ(len(deep_copy_empty), 1)


@test("ListBuiltin", "CopyComplexNested")
def test_copy_complex_nested():
    """测试复杂多层嵌套结构的拷贝"""
    # 创建多层嵌套结构：list[list[dict]]
    complex_list = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]]

    # 浅拷贝：任何嵌套层级的修改都会影响原列表
    shallow_complex = copy.copy(complex_list)

    # 修改最内层的 dict（第三层）
    shallow_complex[0][0]["a"] = 999

    # 原列表的 dict 也被修改了（浅拷贝共享所有内层引用）
    EXPECT_EQ(complex_list[0][0]["a"], 999)

    # 修改中间层的 list（第二层）
    shallow_complex[0].append({"e": 5})

    # 原列表的中间层 list 也被修改了
    EXPECT_EQ(len(complex_list[0]), 2)
    EXPECT_EQ(complex_list[0][1]["e"], 5)

    # 重新创建用于深拷贝测试
    complex_list2 = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]]
    deep_complex = copy.deepcopy(complex_list2)

    # 修改最内层的 dict（第三层）
    deep_complex[0][0]["a"] = 888

    # 原列表的 dict 未被修改（深拷贝是完全独立的）
    EXPECT_EQ(complex_list2[0][0]["a"], 1)
    EXPECT_EQ(deep_complex[0][0]["a"], 888)

    # 修改中间层的 list（第二层）
    deep_complex[0].append({"e": 5})

    # 原列表的中间层 list 未被修改
    EXPECT_EQ(len(complex_list2[0]), 1)
    EXPECT_EQ(len(deep_complex[0]), 2)


@test("ListBuiltin", "DirectNestedDictModification")
def test_direct_nested_dict_modification():
    """测试直接修改嵌套字典"""
    # 创建包含嵌套字典的列表
    data = [
        {"user": {"name": "Alice", "age": 30}},
        {"user": {"name": "Bob", "age": 25}},
    ]

    data[0]["user"]["age"] = 31
    data[0]["user"]["city"] = "Beijing"

    # 验证修改成功
    EXPECT_EQ(data[0]["user"]["age"], 31)
    EXPECT_EQ(data[0]["user"]["city"], "Beijing")
    EXPECT_EQ(data[0]["user"]["name"], "Alice")

    # 验证其他元素未受影响
    EXPECT_EQ(data[1]["user"]["age"], 25)

    # 深拷贝后修改不影响原数据
    data_copy = copy.deepcopy(data)
    data_copy[0]["user"]["age"] = 99

    EXPECT_EQ(data[0]["user"]["age"], 31)  # 原数据未变
    EXPECT_EQ(data_copy[0]["user"]["age"], 99)  # 拷贝已变


# 全局辅助函数（用于测试列表字面值作为参数）
def sum_list_helper(numbers: list[int]) -> int:
    """辅助函数：计算列表和"""
    total = 0
    for num in numbers:
        total = total + num
    return total


def flatten_helper(matrix: list[list[int]]) -> list[int]:
    """辅助函数：展平二维列表"""
    result: list[int] = []
    for row in matrix:
        for num in row:
            result.append(num)
    return result


def get_numbers_helper() -> list[int]:
    """辅助函数：返回数字列表"""
    return [1, 2, 3, 4, 5]


def check_membership_helper(value: int) -> bool:
    """辅助函数：检查成员关系"""
    numbers = [1, 2, 3, 4, 5]
    return value in numbers


@test("ListBuiltin", "ListLiteralAsArgument")
def test_list_literal_as_argument():
    """测试列表字面值作为函数参数"""
    # 直接传递列表字面值作为函数参数
    result: int = sum_list_helper([1, 2, 3, 4, 5])
    EXPECT_EQ(result, 15)

    # 直接传递嵌套列表字面值作为函数参数
    result2: list[int] = flatten_helper([[1, 2], [3, 4], [5, 6]])
    expected2 = [1, 2, 3, 4, 5, 6]
    EXPECT_EQ(result2, expected2)


@test("ListBuiltin", "ListLiteralReturn")
def test_list_literal_return():
    """测试函数返回列表字面值"""
    result: list[int] = get_numbers_helper()
    expected = [1, 2, 3, 4, 5]
    EXPECT_EQ(result, expected)


@test("ListBuiltin", "ListLiteralInCondition")
def test_list_literal_in_condition():
    """测试条件表达式中的列表字面值"""
    EXPECT_TRUE(check_membership_helper(3))
    EXPECT_FALSE(check_membership_helper(10))


@test("ListBuiltin", "SortedFunction")
def test_sorted_function():
    """测试 sorted() 函数（返回新列表，不修改原列表）"""
    numbers = [3, 1, 4, 1, 5, 9, 2, 6]

    # 默认升序排序
    sorted_asc: list[int] = sorted(numbers)
    expected_asc = [1, 1, 2, 3, 4, 5, 6, 9]
    EXPECT_EQ(sorted_asc, expected_asc)

    # 原列表未被修改
    original = [3, 1, 4, 1, 5, 9, 2, 6]
    EXPECT_EQ(numbers, original)

    # 降序排序
    sorted_desc: list[int] = sorted(numbers, reverse=True)
    expected_desc = [9, 6, 5, 4, 3, 2, 1, 1]
    EXPECT_EQ(sorted_desc, expected_desc)

    # 原列表仍然未被修改
    EXPECT_EQ(numbers, original)

    # 测试字符串列表排序
    words = ["banana", "apple", "cherry"]
    sorted_words: list[str] = sorted(words)
    expected_sorted_words = ["apple", "banana", "cherry"]
    EXPECT_EQ(sorted_words, expected_sorted_words)


@test("ListBuiltin", "SumFunction")
def test_sum_function():
    """测试 sum() 函数"""
    # 整数列表求和
    numbers = [1, 2, 3, 4, 5]
    result: int = sum(numbers)
    EXPECT_EQ(result, 15)

    # 带起始值的求和
    result_with_start: int = sum(numbers, 10)
    EXPECT_EQ(result_with_start, 25)

    # 浮点数列表求和
    floats = [1.5, 2.5, 3.0]
    float_result: float = sum(floats)
    EXPECT_EQ(float_result, 7.0)

    # 空列表求和
    empty: list[int] = []
    empty_sum: int = sum(empty)
    EXPECT_EQ(empty_sum, 0)


@test("ListBuiltin", "MinMaxFunctions")
def test_min_max_functions():
    """测试 min() 和 max() 函数"""
    # 整数列表
    numbers = [3, 1, 4, 1, 5, 9, 2, 6]
    min_val: int = min(numbers)
    max_val: int = max(numbers)
    EXPECT_EQ(min_val, 1)
    EXPECT_EQ(max_val, 9)

    # 浮点数列表
    floats: list[float] = [3.14, 2.71, 1.41, 1.73]
    min_float: float = min(floats)
    max_float: float = max(floats)
    EXPECT_EQ(min_float, 1.41)
    EXPECT_EQ(max_float, 3.14)

    # 字符串列表
    words = ["banana", "apple", "cherry"]
    min_word: str = min(words)
    max_word: str = max(words)
    EXPECT_EQ(min_word, "apple")
    EXPECT_EQ(max_word, "cherry")


@test("ListBuiltin", "AnyAllFunctions")
def test_any_all_functions():
    """测试 any() 和 all() 函数"""
    # any() - 至少一个为真
    bool_list1 = [True, False, False]
    bool_list2 = [False, False, False]
    bool_list3 = [True, True, True]

    EXPECT_TRUE(any(bool_list1))
    EXPECT_FALSE(any(bool_list2))
    EXPECT_TRUE(any(bool_list3))

    # all() - 全部为真
    EXPECT_FALSE(all(bool_list1))
    EXPECT_FALSE(all(bool_list2))
    EXPECT_TRUE(all(bool_list3))

    # 空列表的边界情况
    empty_list: list[bool] = []
    EXPECT_FALSE(any(empty_list))
    EXPECT_TRUE(all(empty_list))


@test("ListBuiltin", "EnumerateFunction")
def test_enumerate_function():
    """测试 enumerate() 函数（返回迭代器视图）"""
    # 基本使用：从 0 开始（通过迭代）
    words = ["apple", "banana", "cherry"]
    result: list[tuple[int, str]] = []
    for pair in enumerate(words):
        result.append(pair)

    EXPECT_EQ(len(result), 3)
    EXPECT_EQ(result[0], (0, "apple"))
    EXPECT_EQ(result[1], (1, "banana"))
    EXPECT_EQ(result[2], (2, "cherry"))

    # 使用 start 参数：从指定数字开始
    numbers = [10, 20, 30]
    indexed: list[tuple[int, int]] = []
    for pair in enumerate(numbers, start=1):
        indexed.append(pair)

    EXPECT_EQ(len(indexed), 3)
    EXPECT_EQ(indexed[0], (1, 10))
    EXPECT_EQ(indexed[1], (2, 20))
    EXPECT_EQ(indexed[2], (3, 30))

    # start 参数为负数
    items = ["x", "y", "z"]
    neg_indexed: list[tuple[int, str]] = []
    for pair in enumerate(items, start=-5):
        neg_indexed.append(pair)

    EXPECT_EQ(len(neg_indexed), 3)
    EXPECT_EQ(neg_indexed[0], (-5, "x"))
    EXPECT_EQ(neg_indexed[1], (-4, "y"))
    EXPECT_EQ(neg_indexed[2], (-3, "z"))

    # 空列表
    empty: list[int] = []
    empty_result: list[tuple[int, int]] = []
    for pair in enumerate(empty):
        empty_result.append(pair)
    EXPECT_EQ(len(empty_result), 0)

    # 测试字面量列表（右值重载版本）- 验证临时对象生命周期正确
    literal_result: list[tuple[int, int]] = []
    for pair in enumerate([100, 200, 300], start=10):
        literal_result.append(pair)

    EXPECT_EQ(len(literal_result), 3)
    EXPECT_EQ(literal_result[0], (10, 100))
    EXPECT_EQ(literal_result[1], (11, 200))
    EXPECT_EQ(literal_result[2], (12, 300))


@test("ListBuiltin", "MinMaxListVersion")
def test_min_max_list_version():
    """测试 min() 和 max() 的列表版本（已实现并验证通过）"""
    # 整数列表
    numbers = [10, 3, 7, 1, 9]
    EXPECT_EQ(min(numbers), 1)
    EXPECT_EQ(max(numbers), 10)

    # 浮点数列表
    floats = [1.5, 2.5, 0.5, 3.5]
    EXPECT_EQ(min(floats), 0.5)
    EXPECT_EQ(max(floats), 3.5)

    # 负数列表
    negatives = [-5, -10, -3, -1]
    EXPECT_EQ(min(negatives), -10)
    EXPECT_EQ(max(negatives), -1)


@test("ListBuiltin", "ZipFunction")
def test_zip_function():
    """测试 zip() 函数"""
    numbers = [1, 2, 3]
    letters_idx = [10, 20, 30]
    result_list: list[tuple[int, int]] = []
    for pair in zip(numbers, letters_idx):
        result_list.append(pair)

    EXPECT_EQ(len(result_list), 3)
    EXPECT_EQ(result_list[0], (1, 10))
    EXPECT_EQ(result_list[1], (2, 20))
    EXPECT_EQ(result_list[2], (3, 30))

    # 不同类型的列表
    ids = [1, 2, 3]
    names = ["Alice", "Bob", "Charlie"]
    pairs: list[tuple[int, str]] = []
    for pair in zip(ids, names):
        pairs.append(pair)

    EXPECT_EQ(len(pairs), 3)
    EXPECT_EQ(pairs[0], (1, "Alice"))
    EXPECT_EQ(pairs[1], (2, "Bob"))
    EXPECT_EQ(pairs[2], (3, "Charlie"))

    # 不等长列表（取较短的）
    short_list = [1, 2]
    long_list = [10, 20, 30, 40]
    short_result: list[tuple[int, int]] = []
    for pair in zip(short_list, long_list):
        short_result.append(pair)

    EXPECT_EQ(len(short_result), 2)
    EXPECT_EQ(short_result[0], (1, 10))
    EXPECT_EQ(short_result[1], (2, 20))

    # 空列表
    empty: list[int] = []
    non_empty = [1, 2, 3]
    empty_result: list[tuple[int, int]] = []
    for pair in zip(empty, non_empty):
        empty_result.append(pair)

    EXPECT_EQ(len(empty_result), 0)

    # 测试字面量列表（右值重载版本）- 验证临时对象生命周期正确
    literal_result: list[tuple[int, int]] = []
    for pair in zip([100, 200, 300], [1, 2, 3]):
        literal_result.append(pair)

    EXPECT_EQ(len(literal_result), 3)
    EXPECT_EQ(literal_result[0], (100, 1))
    EXPECT_EQ(literal_result[1], (200, 2))
    EXPECT_EQ(literal_result[2], (300, 3))

    # 混合：一个变量，一个字面量
    var_list = [10, 20, 30]
    mixed_result1: list[tuple[int, int]] = []
    for pair in zip(var_list, [1, 2, 3]):
        mixed_result1.append(pair)

    EXPECT_EQ(len(mixed_result1), 3)
    EXPECT_EQ(mixed_result1[0], (10, 1))
    EXPECT_EQ(mixed_result1[1], (20, 2))
    EXPECT_EQ(mixed_result1[2], (30, 3))

    mixed_result2: list[tuple[int, int]] = []
    for pair in zip([1, 2, 3], var_list):
        mixed_result2.append(pair)

    EXPECT_EQ(len(mixed_result2), 3)
    EXPECT_EQ(mixed_result2[0], (1, 10))
    EXPECT_EQ(mixed_result2[1], (2, 20))
    EXPECT_EQ(mixed_result2[2], (3, 30))


@test("ListBuiltin", "ListAny")
def test_list_any():
    """测试 list[Any] 的弱类型引用赋值行为"""
    original: list[Any] = [1, "2", 3]
    new_list: list[int] = original
    EXPECT_EQ(len(new_list), 3)

    # 弱类型 list[Any] 转换到强类型 list[int] 会引发一次
    # 强制类型转换，字符串会尝试转换为整数
    expected = [1, 2, 3]
    EXPECT_EQ(new_list, expected)

    i = 0
    for val in original:
        # 目前对于弱类型比较宽松，字符串 ”2“ 和整数 2 可以比较成功
        EXPECT_EQ(val, new_list[i])
        i += 1
